import json
import sys
import plotly.graph_objects as go

# 检查是否传入了文件名作为参数
if len(sys.argv) < 2:
    print("请提供JSON文件的路径作为参数.")
    sys.exit(1)

# 从命令行获取文件名
file_name = sys.argv[1]

# 从文件中读取JSON数据
with open(file_name, 'r') as f:
    data = json.load(f)

# 提取points和polygons信息
points = data['navmesh']['points']
polygons = data['navmesh']['merged_polygon_indexs']

# 提取坐标
x_coords = [p['x'] for p in points]
y_coords = [p['y'] for p in points]

# 创建绘图对象
fig = go.Figure()

# 定义多边形的颜色
colors = ['green', 'red']

# 遍历多边形组
for group in polygons:
    for i, polygon in enumerate(group):
        polygon_x = [x_coords[j] for j in polygon] + [x_coords[polygon[0]]]  # 回到起点形成闭合的多边形
        polygon_y = [y_coords[j] for j in polygon] + [y_coords[polygon[0]]]
        
        color_index = 0 if i == 0 else 1
        
        # 绘制多边形
        fig.add_trace(go.Scatter(
            x=polygon_x,
            y=polygon_y,
            fill='toself',
            line=dict(color=colors[color_index]),
            name=f'Polygon {i+1}'
        ))

# 设置布局
fig.update_layout(
    title="Navmesh Polygons",
    xaxis_title="X Axis",
    yaxis_title="Y Axis",
    showlegend=True
)

# 显示图形
fig.show()
